package com.dao.batch.controller;

import com.dao.batch.entity.User;
import com.dao.batch.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2017/9/27.
 */
@RestController
public class UserController {
    private final static Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    UserService userService;
    @GetMapping("users")
    public List<User> findAll() {
        return userService.findAll();
    }
    /**
     * 分⻚页查询所有记录数
     * @return
     */
    @GetMapping("users/page")
    public Page<User> findByPage(@RequestParam(defaultValue = "0")Integer start,
                                 @RequestParam(defaultValue = "10")Integer size) {
        return userService.findByPage(start/size,size);
    }
    /**
     * 根据id查找记录
     * @param id
     * @return
     */
    @GetMapping("users/{id}")
    public User findUser(@PathVariable("id") Integer id) {
        return userService.findUser(id);
    }
    /**
     * 添加⼀一条记录
     * @param user
     * @return
     */
    @PostMapping("users")
    public User addUser(User user) {
        return userService.addUser(user);
    }
    /**
     * 更更新⼀一条记录
     * @param user
     * @return
     */
    @PutMapping("users")
    public User updateUser(User user) {
        return userService.updateUser(user);
    }
    /**
     * 根据id删除⼀一条记录
     * @param id
     */
    @DeleteMapping("users/{id}")
    public void deleteUser(@PathVariable("id") Integer id) {
        userService.deleteUser(id);
    }
}
